import os,time
import logging
project_path=os.path.split(os.path.realpath(__file__))[0]
#生成日志log文件的路径
path_log_path=os.path.join(project_path,'log')
if not os.path.exists(path_log_path):
    os.makedirs(path_log_path)
path_log=os.path.join(path_log_path,"TestLog.log")

#生成文件的路径
path_data_path=os.path.join(project_path,'data')
if not os.path.exists(path_data_path):
    os.makedirs(path_data_path)

class Mylog:
    def do_log(self, msg, level):
        # 创建容器
        my_log = logging.getLogger()
        # 设定等级
        my_log.setLevel("DEBUG")
        # 设置输出格式
        f = logging.Formatter('%(asctime)s - %(levelname)s - %(funcName)s-----日志信息：%(message)s')
        # 创建输出渠道
        ch = logging.StreamHandler()  # 指定输出到控制台
        ch.setLevel("DEBUG")
        ch.setFormatter(f)
        #         fh=logging.FileHandler(r"C:\Users\HS\eclipse-workspace\ExcelLibrary\simple\test.txt",encoding='utf-8')

        fh = logging.FileHandler(path_log)  # 指定输出到指定文件
        fh.setLevel("DEBUG")
        fh.setFormatter(f)
        # 指定输出渠道
        my_log.addHandler(ch)
        my_log.addHandler(fh)
        if level == "DEBUG":
            my_log.debug(msg)
        if level == "INFO":
            my_log.info(msg)
        if level == "WARN":
            my_log.warning(msg)
        if level == "ERROR":
            my_log.error(msg)
        # 关闭渠道
        my_log.removeHandler(ch)
        my_log.removeHandler(fh)

    def debug(self, msg):
        self.do_log(msg, "DEBUG")

    def info(self, msg):
        self.do_log(msg, "INFO")

    def warn(self, msg):
        self.do_log(msg, "WARN")

    def error(self, msg):
        self.do_log(msg, "ERROR")
log=Mylog()
# path = r"D:\testFile"
delend = ".txt"


def counts(n,m):
    log.info(("===============开始往U盘写入并删除%f次%fGB的文件===============")%(float(m),float(n)))
    if float(n) > 1:
        log.error("请输入小于1G的文件大小")
        exit()
    if int(m)>=1024:
        log.error(("此次输入的数量%f大于U盘存储量,请重新输入")%int(m))
        print("你输入的次数超出U盘存储量，请重新输入！")
        exit()
    for i in range(int(m)):
        creatfilesize(n)
        if i==1000:
            del_files(path_data_path,delend)

def creatfilesize(n):
    start = time.time()
    time.sleep(3)
    log.info("开始测试写入"+n+"GB"+"的文件")
    local_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
    f=str(local_time) + ".txt"
    path_data=os.path.join(path_data_path,f)
    bigFile= open(path_data, 'w')
    bigFile.seek(1024*1024*1024*float(n))
    bigFile.write('test')
    bigFile.close()
    end = time.time()
    print("循环运行时间:%.2f秒" % (end - start))
    log.info("写入"+n+"GB"+"的文件成功"+"使用时间:%.2f秒" % (end - start))

#删除
def del_files(path, delend):
    log.info("开始清除U盘数据")
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(delend):
                os.remove(os.path.join(root, name))
                print("delete file :", os.path.join(root, name))
    log.info("U盘数据清除成功！")

if __name__ == '__main__':
    n = input("输入你要生成的文件E大小（单位为GB【建议输入0.5】）:")
    m = input("输入你要执行的次数【建议输入1024】:")
    counts(n,m)